<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" >
      <el-form-item label="项目名称" prop="name" v-show="showSearch">
        <el-input v-model="queryParams.name" placeholder="请输入项目名称" clearable style="width: 240px" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item v-show="showSearch">
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-form-item>
      <!--      <el-form-item >-->
      <!--        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
      <!--      </el-form-item>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="rowList">
      <el-table-column label="项目编号" align="center" prop="id" width="120" fixed="left"/>
      <el-table-column label="项目名称" align="center" prop="name" width="120" />
<!--      <el-table-column label="项目开始日期" align="center" prop="startTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="验收日期" align="center" prop="endTime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="签约公司" align="center" prop="company" width="120" />
      <el-table-column label="结算审核价（元）" align="center" prop="jiesuanshenhe" width="140" >
        <template v-slot="{row}">
          <span>{{row.jiesuanshenhe | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修期" align="center" prop="keep" width="100" />
      <el-table-column label="合同编号" align="center" prop="contractId" width="120" />
      <el-table-column label="合同金额（元）" align="center" prop="amount" width="140" >
        <template v-slot="{row}">
          <span>{{row.amount | currency}}</span>
        </template>
      </el-table-column>
      <el-table-column label="工期" align="center" prop="duration" width="120" />
      <el-table-column label="合同摘要" align="center" prop="abst" min-width="450" />
<!--      <el-table-column label="项目状态" align="center" prop="status" width="120">-->
<!--        <template slot-scope="scope">-->
<!--          <span :style="{'color': colors(scope.row.status)}">{{ scope.row.status }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="请示" prop="yanshi" align="center" :min-width="yanshiImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.yanshis"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="纪要" prop="jiyao" align="center" :min-width="jiyaoImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.jiyaos"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="合同" prop="hetong" align="center" :min-width="hetongImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.hetongs"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="验收报告" prop="yanshou" align="center" :min-width="yanshouImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.yanshous"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="结算审核" prop="jiesuan" align="center" :min-width="jiesuanImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.jiesuans"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="发票" prop="fapiao" align="center" :min-width="fapiaoImageColWidth">-->
<!--        <template v-slot:default="scope">-->
<!--          <el-image v-for="(item,index) in scope.row.fapiaos"-->
<!--                    style="width: 100px; height: 100px; margin-right: 15px"-->
<!--                    :src="baseUrl + item">-->
<!--          </el-image>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="创建人" align="center" prop="createBy" width="120"/>
      <el-table-column label="修改人" align="center" prop="updateBy" width="120"/>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修改时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="200">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDetail(scope.row)"
          >进度记录</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="740px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="140px">

<!--        <el-form-item label="项目合同" prop="contract">-->
<!--          <el-select v-model="form.contract" placeholder="请选择项目合同" clearable :style="{width: '100%'}">-->
<!--            <el-option v-for="(item, index) in contracts" :key="index" :label="item.label"-->
<!--                       :value="item.value"></el-option>-->
<!--          </el-select>-->
<!--        </el-form-item>-->
        <el-row>
          <el-col :span="12">
            <el-form-item label="合同编号" prop="contractId">
              <el-input v-model="form.contractId" placeholder="请输入合同编号" />
            </el-form-item>
            <el-form-item label="签约公司" prop="company">
              <el-input v-model="form.company" placeholder="请输入签约公司" />
            </el-form-item>
<!--            <el-form-item label="项目状态" prop="status">-->
<!--              <el-select v-model="form.status" placeholder="请选择状态" clearable :style="{width: '100%'}">-->
<!--                <el-option v-for="(item, index) in menuOptions" :key="index" :label="item.label" :value="item.value" :disabled="item.disabled"></el-option>-->
<!--              </el-select>-->
<!--            </el-form-item>-->
<!--            <el-form-item label="开始日期" prop="startTime">-->
<!--              <el-date-picker v-model="form.startTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :style="{width: '100%'}" placeholder="请选择开始时间" clearable></el-date-picker>-->
<!--            </el-form-item>-->
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入项目名称" />
            </el-form-item>
            <el-form-item label="合同金额（元）" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入合同金额" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="合同摘要" prop="abst">
          <el-input v-model="form.abst" type="textarea" placeholder="请输入合同摘要"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="工期" prop="duration">
              <el-input v-model="form.duration" placeholder="请输入工期" />
            </el-form-item>
            <el-form-item label="验收日期" prop="endTime">
              <el-date-picker v-model="form.endTime" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :style="{width: '100%'}" placeholder="请选择计划结束时间" clearable></el-date-picker>
            </el-form-item>
            <el-form-item label="剩余付款次数" prop="remain">
              <el-input v-model="form.remain" placeholder="请输入剩余付款次数" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算审核价（元）" prop="jiesuanshenhe">
              <el-input v-model="form.jiesuanshenhe" placeholder="请输入结算审核价" />
            </el-form-item>
            <el-form-item label="保修期" prop="keep">
              <el-input v-model="form.keep" placeholder="请输入保修期" />
            </el-form-item>
            <el-form-item label="付款总次数" prop="total">
              <el-input v-model="form.total" placeholder="请输入付款总次数" />
            </el-form-item>
          </el-col>

        </el-row>
<!--        <el-form-item label="请示" prop="yanshi">-->
<!--          <ImageUpload-->
<!--            v-model="form.yanshi"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="纪要" prop="jiyao">-->
<!--          <ImageUpload-->
<!--            v-model="form.jiyao"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="合同" prop="hetong">-->
<!--          <ImageUpload-->
<!--            v-model="form.hetong"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="验收报告" prop="yanshou">-->
<!--          <ImageUpload-->
<!--            v-model="form.yanshou"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="结算审核" prop="jiesuan">-->
<!--          <ImageUpload-->
<!--            v-model="form.jiesuan"/>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="发票" prop="fapiao">-->
<!--          <ImageUpload-->
<!--            v-model="form.fapiao"/>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, get, del, add, update } from "@/api/social/program/program";
import { listAll as listAllCompany } from "@/api/social/examine/company";
import { listProgram as listAllContract} from "@/api/social/contract/contract";
import { getDicts } from "@/api/system/dict/data";
import {blurRule, changeRule} from "@/utils/formRules";
export default {
  name: "Program",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      baseUrl: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 非单个禁用
      single: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      rowList: [],
      companies: [],
      contracts: [],
      yanshiImageColWidth: 140,
      jiyaoImageColWidth: 140,
      hetongImageColWidth: 140,
      yanshouImageColWidth: 140,
      jiesuanImageColWidth: 140,
      fapiaoImageColWidth: 140,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      menuOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        contractId:blurRule('请输入合同编号'),
        name:blurRule('请输入项目名称'),
      }
    };
  },
  created() {
    this.getList();
    this.getType();
  },
  methods: {
    handleExport() {
      this.download('social/program/program/export', {
        ...this.queryParams
      }, `项目列表_${new Date().getTime()}.xlsx`)
    },
    async getType(){
      const {data} = await getDicts('item_status')
      this.menuOptions = data.map(item=>({label:item.dictLabel,value:item.dictValue}))
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      list(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        // let maxyanshi = 1;
        // let maxjiyao = 1;
        // let maxhetong = 1;
        // let maxyanshou = 1;
        // let maxjiesuan = 1;
        // let maxfapiao = 1;
        // for (var i = 0; i < response.rows.length; i++) {
        //   response.rows[i].yanshis = [];
        //   response.rows[i].jiyaos = [];
        //   response.rows[i].hetongs = [];
        //   response.rows[i].yanshous = [];
        //   response.rows[i].jiesuans = [];
        //   response.rows[i].fapiaos = [];
        //   if (response.rows[i].yanshi != null && response.rows[i].yanshi != undefined) {
        //     response.rows[i].yanshis = response.rows[i].yanshi.split(",");
        //   }
        //   if (response.rows[i].yanshis.length > maxyanshi) {
        //     maxyanshi = response.rows[i].yanshis.length;
        //   }
        //
        //   if (response.rows[i].jiyao != null && response.rows[i].jiyao != undefined) {
        //     response.rows[i].jiyaos = response.rows[i].jiyao.split(",");
        //   }
        //   if (response.rows[i].jiyaos.length > maxyanshi) {
        //     maxjiyao = response.rows[i].jiyaos.length;
        //   }
        //
        //   if (response.rows[i].hetong != null && response.rows[i].hetong != undefined) {
        //     response.rows[i].hetongs = response.rows[i].hetong.split(",");
        //   }
        //   if (response.rows[i].yanshis.length > maxyanshi) {
        //     maxhetong = response.rows[i].yanshis.length;
        //   }
        //
        //   if (response.rows[i].yanshou != null && response.rows[i].yanshou != undefined) {
        //     response.rows[i].yanshous = response.rows[i].yanshou.split(",");
        //   }
        //   if (response.rows[i].yanshous.length > maxyanshi) {
        //     maxyanshou = response.rows[i].yanshous.length;
        //   }
        //
        //   if (response.rows[i].jiesuan != null && response.rows[i].jiesuan != undefined) {
        //     response.rows[i].jiesuans = response.rows[i].jiesuan.split(",");
        //   }
        //   if (response.rows[i].jiesuans.length > maxyanshi) {
        //     maxjiesuan = response.rows[i].jiesuans.length;
        //   }
        //
        //   if (response.rows[i].fapiao != null && response.rows[i].fapiao != undefined) {
        //     response.rows[i].fapiaos = response.rows[i].fapiao.split(",");
        //   }
        //   if (response.rows[i].fapiaos.length > maxyanshi) {
        //     maxfapiao = response.rows[i].fapiaos.length;
        //   }
        // }
        // this.yanshiImageColWidth = maxyanshi * 140;
        // this.jiyaoImageColWidth = maxjiyao * 140;
        // this.hetongImageColWidth = maxhetong * 140;
        // this.yanshouImageColWidth = maxyanshou * 140;
        // this.jiesuanImageColWidth = maxjiesuan * 140;
        // this.fapiaoImageColWidth = maxfapiao * 140;
        this.rowList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
      listAllContract().then(res => {
        for (var i = 0; i < res.rows.length; i++) {
          this.contracts.push({
            label: res.rows[i].name,
            value: res.rows[i].id
          });
        }
      })
      listAllCompany().then(res => {
        for (var i = 0; i < res.rows.length; i++) {
          this.companies.push({
            label: res.rows[i].name,
            value: res.rows[i].name
          });
        }
      });
    },
    colors(status) {
      if (status === "已完成") {
        return '#2a9c4a'
      } else if (status === "施工中" || status == "验收中") {
        return '#fe9f29'
      }
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮（数据权限）
    cancelDataScope() {
      this.openDataScope = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      get(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            update(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的数据项？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleDetail(row) {
      const id = row.id;
      this.$router.push("/social/program/baseinfo/detail/" + id);
    }
  }
};
</script>
